﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class AddOrUpdateReportViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        private int totalNum;

        public int TotalNum
        {
            get { return totalNum; }
            set { totalNum = value; RaisePropertyChanged(); GetDataInfo(value); }
        }

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }


        private string number;

        public string Number
        {
            get { return number; }
            set { number = value; RaisePropertyChanged(); }
        }

        private string reportTime;

        public string ReportTime
        {
            get { return reportTime; }
            set { reportTime = value; RaisePropertyChanged(); }
        }

        

        private int reportUnitSelected;

        public int ReportUnitSelected
        {
            get { return reportUnitSelected; }
            set { reportUnitSelected = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> reportUnitList;

        public ObservableCollection<string> ReportUnitList
        {
            get { return reportUnitList; }
            set { reportUnitList = value; RaisePropertyChanged(); }
        }

        private string msData;

        public string MSData
        {
            get { return msData; }
            set { msData = value; RaisePropertyChanged(); }
        }

        private int reportFlagSelected;

        public int ReportFlagSelected
        {
            get { return reportFlagSelected; }
            set { reportFlagSelected = value; RaisePropertyChanged(); }
        }

      

        private string reportNumber;

        public string ReportNumber
        {
            get { return reportNumber; }
            set { reportNumber = value; RaisePropertyChanged(); }
        }

        private int reportTypeSelected;

        public int ReportTypeSelected
        {
            get { return reportTypeSelected; }
            set { reportTypeSelected = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> reportTypeList;

        public ObservableCollection<string> ReportTypeList
        {
            get { return reportTypeList; }
            set { reportTypeList = value; RaisePropertyChanged(); }
        }

        private string data;

        public string Data
        {
            get { return data; }
            set { data = value; RaisePropertyChanged(); }
        }

        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); GroupName = $"第{value + 1}个配置单元"; UpDataInfo(); }
        }

        private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

        public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
            get { return gWCmdMakeUpList; }
            set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<SingleData> oADList;

        public ObservableCollection<SingleData> OADList
        {
            get { return oADList; }
            set { oADList = value; RaisePropertyChanged(); }
        }


        public AddOrUpdateReportViewModel()
        {
            TotalNumList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            ReportUnitList = new ObservableCollection<string>() { "秒", "分", "时", "日", "月", "年" };
            ReportTypeList = new ObservableCollection<string>() { "OAD", "RecordData" };
            OADList = new ObservableCollection<SingleData>() { };
        }


        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("601C7F00", total));
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            SelectedIndex = 0;
            GWCmdMakeUp = null;

        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp == null)
            {
                return;
            }
            try
            {
                OADList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
                if (datas.Length < 6)
                {
                    return;
                }
                Number = datas[0];

                var strings = datas[1].Split(',');
                for (int i = 0; i < strings.Length; i++)
                {
                    OADList.Add(new SingleData { Name = strings[i] });
                }
                ReportTime = datas[2];
                ReportUnitSelected = int.Parse(datas[3]);
                ReportNumber = datas[4];
                ReportTypeSelected = int.Parse(datas[5]);
                Data = datas[6];

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp == null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(int.Parse(Number).ToString());
            stringBuilder.Append(";");
            for (int i = 0; i < OADList.Count; i++)
            {
                stringBuilder.Append(OADList[i].Name);
                if (i+1< OADList.Count)
                {
                    stringBuilder.Append(",");
                }
            }
            stringBuilder.Append(";");
            stringBuilder.Append(ReportTime);
            stringBuilder.Append(";");
            stringBuilder.Append(ReportUnitSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(ReportNumber);
            stringBuilder.Append(";");
            stringBuilder.Append(ReportTypeSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Data);
            GWCmdMakeUp.DataValue = stringBuilder.ToString();
            gWCmdMakeUpService.Update(GWCmdMakeUp);
        }

        public RelayCommand MSDataCommand
        {
            get
            {
                var command = new RelayCommand(() =>
                {
                    MSDataModel mSDataModel = new MSDataModel();
                    mSDataModel.Data = MSData;
                    SimpleIoc.Default.Unregister<MSDataModel>();
                    SimpleIoc.Default.Unregister<MeterListViewModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<MeterListViewModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register(() => mSDataModel);
                    
                });
                return command;
            }
        }
    }
}
